package nsoft.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;

public abstract class FormatUtil {
	private static final String EMPTY = "";
	static private DecimalFormat nbFmt;
	public static SimpleDateFormat dateFormat;// = new
	// SimpleDateFormat("dd/MM/yyyy");
	static {
		//TODO To be loaded from properties file.
		nbFmt = new DecimalFormat("#.##");
		DecimalFormatSymbols nbSymbols = new DecimalFormatSymbols();
		nbFmt.setDecimalFormatSymbols(nbSymbols);
		dateFormat = new SimpleDateFormat("dd/MM/yyyy");
	}

	public static String format(BigDecimal number) {
		if (number == null) {
			return EMPTY;
		}
		return nbFmt.format(number.doubleValue());
	}

	public static String format(Long number) {
		if (number == null) {
			return EMPTY;
		}
		return nbFmt.format(number.doubleValue());
	}

	public static String format(Double number) {
		if (number == null) {
			return EMPTY;
		}
		return nbFmt.format(number.doubleValue());
	}

	public static String format(Integer number) {
		if (number == null) {
			return EMPTY;
		}
		return nbFmt.format(number.doubleValue());
	}

	public static String format(Object value) {
		if (value == null) {
			return EMPTY;
		}
		if (value instanceof Integer) {
			return FormatUtil.format((Integer) value);
		}
		if (value instanceof Long) {
			return FormatUtil.format((Long) value);
		}
		if (value instanceof Double) {
			return FormatUtil.format((Double) value);
		}
		if (value instanceof BigDecimal) {
			return FormatUtil.format((BigDecimal) value);
		}
		if (value instanceof Date) {
			return FormatUtil.format((Date) value);
		}
		if (value instanceof Boolean) {
			return FormatUtil.format((Boolean) value);
		}
		return value.toString();
	}

	public static String format(Date date) {
		return dateFormat.format(date);
	}

	public static String format(Boolean value) {
		return ((Boolean) value) ? "Yes" : "No";
	}
}
